<template>
    <div class="singer" ref="singer">
      <listview :data="singer" @select="selectSinger" ref="list"></listview>
      <router-view></router-view>
    </div>
</template>
<script>
import { getSinger } from 'api/singer.js'
import { ERR_OK } from 'api/config.js'
import Singer from 'common/js/singer.js'
import listview from 'base/listview/listview'
import {mapMutations} from 'vuex'
import {playlistMixin} from 'common/js/mixin'

const HOT_NAME = '热门'
const HOT_SINGER_LEN = 10

export default {
  mixins: [playlistMixin],
  data() {
    return {
      singer: []
    }
  },
  mounted() {
    this._getSinger()
  },
  methods: {
    handlePlaylist(playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''
      this.$refs.singer.style.bottom = bottom
      this.$refs.list.refresh()
    },
    selectSinger(singer) {
      // 改变路由指向
      this.$router.push({
        path: `/singer/${singer.id}`
      })
      this.setSinger(singer)
    },
    _getSinger() {
      getSinger().then((res) => {
        if (res.code === ERR_OK) {
          /* list数组
          [
            {
            Farea: "1",
            Fattribute_3: "3",
            Fattribute_4: "0",
            Fgenre: "0",
            Findex: "X",
            Fother_name: "Joker",
            Fsinger_id: "5062",
            Fsinger_mid: "002J4UUk29y8BY",
            Fsinger_name: "薛之谦",
            Fsinger_tag: "541,555",
            Fsort: "1",
            Ftrend: "0",
            Ftype: "0",
            voc: "0"
            }
          ]
          */
          this.singer = this._normalizeSinger(res.data.list)
        }
      })
    },
    // 对取得的音乐数据进行筛选及排序
    _normalizeSinger(list) {
      let map = {
        hot: {
          title: HOT_NAME,
          items: []
        }
      }
      /*
      map {
        hot: {
          title: HOT_NAME,
          item: []
        }
        A: {
          title: 'A'
          item: []
        }
      }
      */
      list.forEach((item, index) => {
        // 往map里面的热门添加前10条数据
        if (index < HOT_SINGER_LEN) {
          map.hot.items.push(new Singer({
            id: item.Fsinger_mid,
            name: item.Fsinger_name
          }))
        }
        // 判断Findex在不在
        let key = item.Findex
        if (!map[key]) {
          map[key] = {
            title: key,
            items: []
          }
        }
        map[key].items.push(new Singer({
          id: item.Fsinger_mid,
          name: item.Fsinger_name
        }))
      })
      // 对 map里面的数据进行有序化
      let hot = []
      let ret = []
      for (var key in map) {
        if (map[key].title.match(/[a-zA-Z]/)) {
          ret.push(map[key])
        } else if (map[key].title === HOT_NAME) {
          hot.push(map[key])
        }
      }
      ret.sort((a, b) => {
        return a.title.charCodeAt(0) - b.title.charCodeAt(b)
      })
      return hot.concat(ret)
    },
    ...mapMutations({
      setSinger: 'SET_SINGER'
    })
  },
  components: {
    listview
  }
}
</script>
<style lang="stylus" scoped>
.singer
  position: fixed
  top: 88px
  bottom: 0
  width: 100%
  overflow: hidden
</style>